<template>
  <!-- 用户管理 -->
  <div class="user_list">
    <div class="btns">
      <el-form :inline="true" size="small">
        <el-button type="primary" size="small" @click="toAdd">添加</el-button>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-form-item>
          <el-input placeholder="请输入用户名" clearable v-model="param.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="param.rolename" placeholder="请选择角色" clearable>
            <el-option v-for="r in roles" :key="r.id" :label="r.name" :value="r.name"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="text" size="mini" @click="loadUsers">搜索</el-button>
      </el-form>
    </div>
    <el-table :data="users.list" size="small" v-loading="loading">
      <el-table-column type="index" :index="1" label="序号"/>
      <el-table-column prop="username" label="用户名" width="200" align="center"/>
      <el-table-column label="角色" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.role_name }}
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="60">
        <template slot-scope="scope">
          <span v-if="scope.row.gender ==='male'">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态"/>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click.prevent="toSetRole(scope.row)">设置</el-button>
          <el-button type="text" size="mini" @click.prevent="deleteHandler(scope.row.id)">移除</el-button>
          <el-button type="text" size="mini" @click.prevent="toDetails(scope.row)">详情</el-button>
          <el-button type="text" size="mini" @click.prevent="toEdit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination background layout="prev, pager, next" small
      :hide-on-single-page="true"
      :total="users.total"
      :page-size="users.pageSize"
      :current-page="users.page"
      @current-change="currentChangeHandler">
    </el-pagination>
    <!-- 分页 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="40%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form ref="user_form" :model="form" :rules="rules">
          <el-form-item label="用户名" label-width="80px" prop="username">
            <el-input v-model="form.username" autocomplete="off" v-if="form.id" disabled/>
            <el-input v-model="form.username" autocomplete="off" v-else/>
          </el-form-item>
          <el-form-item label="密码" label-width="80px" prop="password">
            <el-input v-model="form.password" autocomplete="off" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="role_visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="saveUserHandler">确 定</el-button>
      </span>
    </Briupdrawer>
    <!-- 抽屉结束 -->
    <!-- 角色模态框 -->
    <el-dialog title="设置角色" :visible.sync="role_visible" class="customer_modal">
      <el-form :model="user">
        <el-form-item label="用户名" label-width="80px">
          {{ user.username }}
        </el-form-item>
        <el-form-item label="角色" label-width="80px">
          <el-cascader filterable v-model="user.roles" :options="roles" :props="props" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="role_visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="setRolesHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>
<script>
import { get, post, del } from '@/utils/request'

import qs from 'querystring'
export default {
  data() {
    return {
      form: {},
      loading:false,
      visible: false,
      role_visible: false,
      title: '添加用户',
      user: {},
      users: [],
      roles: [],
      param:{
        page:1,
        pageSize:13
      },
      props: { multiple: true, value: 'id', label: 'name', emitPath: false },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.loadUsers()
    this.loadRoles()
  },
  methods: {
    loadRoles() {
      get('/role/findAll')
        .then(response => {
          this.roles = response.data
        })
    },
    saveUserHandler() {
      this.$refs['user_form'].validate((valid) => {
        if (valid) {
          const url = '/baseUser/saveOrUpdate'
          post(url, this.form)
            .then(response => {
              this.visible = false
              this.$message({ message: response.message, type: 'success' })
              this.loadUsers()
            })
        } else {
          return false
        }
      })
    },
    toAdd() {
      this.form = {}
      this.visible = true
    },
    loadUsers() {
      this.loading = true;
      get('/baseUser/pageQuery',this.param)
        .then(response => {
          response.data.list.forEach(item => {
            item.role_name = item.roles.map(r => r.name).join(',')
            item.roles = item.roles.map(r => r.id)
          })
          this.users = response.data
          this.loading = false;
        })
    },
    deleteHandler(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = '/baseUser/deleteById'
        get(url, { id }).then(response => {
          this.$message({ type: 'success', message: response.message })
          this.loadUsers()
        })
      })
    },
    toEdit(record) {
      this.form = record
      this.visible = true
    },
    toSetRole(record) {
      // 初始化角色
      // record.roles = [];
      this.user = record
      this.role_visible = true
    },
    toDetails(row) {
      this.$router.push({
        // path:'/sys/user/Details',
        name: '_sys_user_Details',
        query: row
      })
    },
    setRolesHandler() {
      post('/baseUser/setRoles', {
        id: this.user.id,
        roles: this.user.roles
      })
        .then(response => {
          this.role_visible = false
          this.$message({ message: response.message, type: 'success' })
          this.loadUsers()
        })
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadUsers();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['user_form'].resetFields();
    },
    //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val
    },
  }
}
</script>
